<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define("DEBUG",false);
class goa extends CI_Controller {

	public function index()
	{
		exit();
	}
	public function tour()
	{
	
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/javascript");
		$client=(isset($_GET['agent'])?(int)$_GET['agent']:0);
		$agent=(isset($_GET['oto'])?(int)$_GET['oto']:0); 
		$callback = (isset($_GET["callback"])?$_GET["callback"]:'');
		$type=3;
		if($client==0||$agent==0||$callback=='')exit();
		
		
		if(!file_exists("cache/t".$client."_".$agent.".json") or DEBUG)
			{
				$this->load->model('json');
				$cont = $this->json->getlist($agent,$client,$type);
				$fh = fopen("cache/t".$client."_".$agent.".json", "w");
				fwrite($fh,$cont);
				fclose($fh);
				echo $callback.'('.$cont.');';
			}
			else
			{
				$filename = "cache/t".$client."_".$agent.".json";
				$handle = fopen($filename, "r");
				$contents = fread($handle, filesize($filename));
				echo $callback.'('.$contents.')';
				
			}
		//header ("HTTP/1.1 200");
		//
	}
	public function tour_full()
	{
		//$id=(int)$_GET['id'];
		$id=(isset($_GET['id'])?(int)$_GET['id']:0);
		if($id==0)exit();
		$callback = $_GET["callback"];
		$type=3;
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		if(!file_exists("cache/tf".$id.".json") or DEBUG)
		{
			$this->load->model('json');
			$cont = $this->json->getfull($type,$id);
			$fh = fopen("cache/tf".$id.".json", "w");
			fwrite($fh,$cont);
			fclose($fh);
			echo $callback.'('.$cont.')';
		}
		else
		{
			//read the file
			$filename = "cache/tf".$id.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.')';
		}
	}
	public function hotels()
	{
		$client=29;
		$agent=(isset($_GET['oto'])?(int)$_GET['oto']:0);
		$callback = (isset($_GET["callback"])?$_GET["callback"]:'');
		
		if($client==0||$agent==0||$callback=='')exit();
		
		$type=1;
		header('Access-Control-Allow-Origin: *');
		$this->output->set_header("HTTP/1.1 200 OK");
		//for IE
		if(!file_exists("cache/h".$client."_".$agent.".json") or DEBUG)
		{
			$this->load->model('goa_json','json');
			$cont = $this->json->gethotellist($client,$agent,$type);
			$fh = fopen("cache/h".$client."_".$agent.".json", "w");
			fwrite($fh,$cont);
			fclose($fh);
			echo $callback.'('.$cont.')';
		}
		else
		{
			//read the file
			$filename = "cache/h".$client."_".$agent.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.');';
		}
	}
	function price_hotel()
	{
		$date1=(isset($_GET['date1'])) ? mysql_escape_string(htmlspecialchars($_GET['date1'])) : 0;
		$date2=(isset($_GET['date2'])) ? mysql_escape_string(htmlspecialchars($_GET['date2'])) : 0;
		$oto=(isset($_GET['id'])) ? (int)($_GET['id']) : 0;
		$type=(isset($_GET['type'])) ? (int)($_GET['type']) : 0;
		$callback = (isset($_GET["callback"])) ? $_GET["callback"] : 0;
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('goa_json');
		
		$cont=$this->goa_json->price($date1,$date2,29,$type);
		echo $callback.'('.$cont.')';
	}
	public function hotel_full()
	{
		$id=(isset($_GET['id'])?(int)$_GET['id']:0);
		$callback = (isset($_GET["callback"])?$_GET["callback"]:'');
		$id=(int)$_GET['id'];
		$callback = $_GET["callback"];
		if($id==0)exit();
		$type=1;
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		if(!file_exists("cache/hf".$id.".json") or DEBUG)
		{
			$this->load->model('goa_json','json');
			$cont = $this->json->gethotelfull($id);
			$fh = fopen("cache/hf".$id.".json", "w");
			fwrite($fh,$cont);
			fclose($fh);
			echo $callback.'('.$cont.')';
		}
		else
		{
			//read the file
			$filename = "cache/hf".$id.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.')';
		}
	
	
	}
}